package com.yuelan.order.producer;

import com.yuelan.common.core.exception.ServiceException;
import com.yuelan.system.api.domain.RentPayOrder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;

import static com.yuelan.order.config.RabbitConfig.ORDER_DELAY_EXCHANGE;
import static com.yuelan.order.config.RabbitConfig.ORDER_DELAY_QUEUE_ROUTING_KEY;

/**
 * @description: 订单 延迟消息 生产者
 * @Author: ZhaoYi
 * @date: 24-7-1 6:28
 */

//@Component
public class OrderDelayMessageProducer {

    @Resource
    private RabbitTemplate rabbitTemplate;

    public String send(RentPayOrder message, String delayTime) {
        try {
            rabbitTemplate.convertAndSend(ORDER_DELAY_EXCHANGE, ORDER_DELAY_QUEUE_ROUTING_KEY, message, msg -> {
                // 设置消息的到期时间
                msg.getMessageProperties().setExpiration(delayTime);
                return msg;
            });
        }
        catch (Exception e) {
            throw new ServiceException("订单创建消息发送失败");
        }
        return "订单消费成功";
    }

}
